****Code to grab means and standard deviations for the control group for each outcome and each year

*single_mean_age multi_mean_age multi_num_vehicle num_new max_veh_age min_miles num_vehicles num_newly_bought sum_vmt oldest_vmt all_hh_new_veh allhh_new_bought

cd "/Users/connorforsythe/Library/CloudStorage/Box-Box/CMU/Marginal Mileage Project/Publication Replication Code/JAERE/NHTSAnalysis"

***Doing it for the year 2017

****Reading in the data
insheet using "CleanData/NHTS2017.csv", comma clear names

**Creating reatment variable
gen nosafetyweighted = 1-hassafetyweighted

**Dropping if removal was 
drop if treatmentyearweighted>=(2017-2) & treatmentyearweighted<=(2017+2)

**Renaming
rename single_vehicle_mean_ageweighted single_mean_age
rename multi_vehicle_mean_ageweighted multi_mean_age
rename multi_vehicle_num_vehiclesweight multi_num_vehicle
rename num_new_vehweighted num_new
rename max_veh_ageweighted max_veh_age
rename min_milesweighted min_miles
rename num_vehiclesweighted num_vehicles
rename num_newly_bought_vehweighted num_newly_bought
rename sum_total_milesweighted sum_vmt
rename oldest_veh_travelweighted oldest_vmt
rename all_hh_new_vehweighted all_hh_new_veh
rename all_hh_newly_bought_vehweighted allhh_new_bought


****Looping over outcome variables
foreach outcome in single_mean_age multi_mean_age multi_num_vehicle num_new max_veh_age min_miles num_vehicles num_newly_bought sum_vmt oldest_vmt all_hh_new_veh allhh_new_bought{
	quietly{
		**Running PSM
		teffects psmatch (`outcome') (nosafetyweighted hhsizeweighted drvrcntweighted incomeweighted hbppopdnweighted wrkcountweighted numadltweighted youngchildweighted ageweighted), ate generate(nummatch)
		matrix b  = e(b)
		matrix V = e(V)
		local tempT = e(b)[1,1]/sqrt(e(V)[1,1])
		local tempP = normal(-abs(`tempT'))*2
		if(`tempP'<0.01){
			local tempStars = "***"
		} 
		else if(`tempP'<0.05){
			local tempStars = "**"
		} 
		else if(`tempP'<0.1){
			local tempStars = "*"
		} 
		else{
			local tempStars = ""
		}	
		
		if("`outcome'"=="num_new" | "`outcome'"=="all_hh_new_veh" | "`outcome'"=="allhh_new_bought"){
			local floatFormat = "%10.3fc"
			local roundamount = 0.001
		}
		else{
			local floatFormat = "%10.2fc"
			local roundamount = 0.01
		}	
		
		**Calculating potential outcomes (only need PO under no treatment)
		gen potential_outcome1 = `outcome' if nosafetyweighted==1
		gen potential_outcome0 = `outcome' if nosafetyweighted==0

		**Looping over observations to grab the relevant value
		forvalues n = 1(1)50{
			gen temp = `outcome' if _n==`n'
			egen value`n' = mean(temp)
			drop temp	
		}

		**Looping over observations to replace with the matched value	
		forvalues n = 1(1)50{
			replace potential_outcome1 = value`n' if (nummatch==`n') & (nosafetyweighted==0)
			replace potential_outcome0 = value`n' if (nummatch==`n') & (nosafetyweighted==1)
			drop value`n'
		}

		sum potential_outcome0
	}
	local `outcome'_d_m_17 = string(round(b[1,1],`roundamount'),"`floatFormat'") + "`tempStars'"
	local `outcome'_diff_se_2017 = string(round(sqrt(V[1,1]),`roundamount'),"`floatFormat'")
	
	quietly sum potential_outcome0
	local `outcome'_c_m_17 = string(round(`r(mean)',`roundamount'),"`floatFormat'")
	local `outcome'_c_sd_17 = string(round(`r(sd)',`roundamount'),"`floatFormat'")

	drop potential_outcome1 potential_outcome0 nummatch
}




***Doing it for the year 2009

****Reading in the data
insheet using "CleanData/NHTS2009.csv", comma clear names

**Creating reatment variable
gen nosafetyweighted = 1-hassafetyweighted

**Dropping if removal was 
drop if treatmentyearweighted>=(2009-2) & treatmentyearweighted<=(2009+2)

**Renaming
rename single_vehicle_mean_ageweighted single_mean_age
rename multi_vehicle_mean_ageweighted multi_mean_age
rename multi_vehicle_num_vehiclesweight multi_num_vehicle
rename num_new_vehweighted num_new
rename max_veh_ageweighted max_veh_age
rename min_milesweighted min_miles
rename num_vehiclesweighted num_vehicles
rename num_newly_bought_vehweighted num_newly_bought
rename sum_total_milesweighted sum_vmt
rename oldest_veh_travelweighted oldest_vmt
rename all_hh_new_vehweighted all_hh_new_veh
rename all_hh_newly_bought_vehweighted allhh_new_bought


****Looping over outcome variables
foreach outcome in single_mean_age multi_mean_age multi_num_vehicle num_new max_veh_age min_miles num_vehicles num_newly_bought sum_vmt oldest_vmt all_hh_new_veh allhh_new_bought{
	quietly{
		**Running PSM
		teffects psmatch (`outcome') (nosafetyweighted hhsizeweighted drvrcntweighted incomeweighted hbppopdnweighted wrkcountweighted numadltweighted ageweighted), ate generate(nummatch)
		matrix b = e(b)
		matrix V = e(V)
		local tempT = e(b)[1,1]/sqrt(e(V)[1,1])
		local tempP = normal(-abs(`tempT'))*2
		if(`tempP'<0.01){
			local tempStars = "***"
		} 
		else if(`tempP'<0.05){
			local tempStars = "**"
		} 
		else if(`tempP'<0.1){
			local tempStars = "*"
		} 
		else{
			local tempStars = ""
		}
		
		if("`outcome'"=="num_new" | "`outcome'"=="all_hh_new_veh" | "`outcome'"=="allhh_new_bought"){
			local floatFormat = "%10.3fc"
			local roundamount = 0.001
		}
		else{
			local floatFormat = "%10.2fc"
			local roundamount = 0.01
		}	
		
		**Calculating potential outcomes (only need PO under no treatment)
		gen potential_outcome1 = `outcome' if nosafetyweighted==1
		gen potential_outcome0 = `outcome' if nosafetyweighted==0

		**Looping over observations to grab the relevant value
		forvalues n = 1(1)50{
			gen temp = `outcome' if _n==`n'
			egen value`n' = mean(temp)
			drop temp	
		}

		**Looping over observations to replace with the matched value	
		forvalues n = 1(1)50{
			replace potential_outcome1 = value`n' if (nummatch==`n') & (nosafetyweighted==0)
			replace potential_outcome0 = value`n' if (nummatch==`n') & (nosafetyweighted==1)
			drop value`n'
		}

		sum potential_outcome0
	}
	local `outcome'_d_m_09 = string(round(b[1,1],`roundamount'),"`floatFormat'") + "`tempStars'"
	local `outcome'_d_se_09 = string(round(sqrt(V[1,1]),`roundamount'),"`floatFormat'")
	
	quietly sum potential_outcome0
	local `outcome'_c_m_09 = string(round(`r(mean)',`roundamount'),"`floatFormat'")
	local `outcome'_c_sd_09 = string(round(`r(sd)',`roundamount'),"`floatFormat'")

	drop potential_outcome1 potential_outcome0 nummatch
}




***Inputting Stats
file open results_table using "Tables/NHTS.tex", write text replace

**num_vehicles
file write results_table "All Households" _tab
file write results_table " & " _tab
file write results_table "Vehicle Count" _tab
file write results_table " & " _tab
file write results_table "`num_vehicles_c_m_09'" _tab
file write results_table " & " _tab
file write results_table "`num_vehicles_d_m_09'"  _tab
file write results_table " & " _tab
file write results_table "`num_vehicles_c_m_17'" _tab
file write results_table " & " _tab
file write results_table "`num_vehicles_d_m_17'"  _tab
file write results_table " \\ " _n
		
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "[`num_vehicles_c_sd_09']" _tab
file write results_table " & " _tab
file write results_table "(`num_vehicles_d_se_09')"  _tab
file write results_table " & " _tab
file write results_table "[`num_vehicles_c_sd_17']" _tab
file write results_table " & " _tab
file write results_table "(`num_vehicles_diff_se_2017')"  _tab
file write results_table " \\ " _n

*Blank row
file write results_table "\vspace{-0.2cm}" _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " \\ " _n


**sum_vmt
file write results_table "All Households" _tab
file write results_table " & " _tab
file write results_table "Aggregate Household VMT" _tab
file write results_table " & " _tab
file write results_table "`sum_vmt_c_m_09'" _tab
file write results_table " & " _tab
file write results_table "`sum_vmt_d_m_09'"  _tab
file write results_table " & " _tab
file write results_table "`sum_vmt_c_m_17'" _tab
file write results_table " & " _tab
file write results_table "`sum_vmt_d_m_17'"  _tab
file write results_table " \\ " _n
		
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "[`sum_vmt_c_sd_09']" _tab
file write results_table " & " _tab
file write results_table "(`sum_vmt_d_se_09')"  _tab
file write results_table " & " _tab
file write results_table "[`sum_vmt_c_sd_17']" _tab
file write results_table " & " _tab
file write results_table "(`sum_vmt_diff_se_2017')"  _tab
file write results_table " \\ " _n

*Blank row
file write results_table "\vspace{-0.2cm}" _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " \\ " _n


**all_hh_new_veh
file write results_table "All Households" _tab
file write results_table " & " _tab
file write results_table "New Vehicles Count" _tab
file write results_table " & " _tab
file write results_table "`all_hh_new_veh_c_m_09'" _tab
file write results_table " & " _tab
file write results_table "`all_hh_new_veh_d_m_09'"  _tab
file write results_table " & " _tab
file write results_table "`all_hh_new_veh_c_m_17'" _tab
file write results_table " & " _tab
file write results_table "`all_hh_new_veh_d_m_17'"  _tab
file write results_table " \\ " _n
		
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "[`all_hh_new_veh_c_sd_09']" _tab
file write results_table " & " _tab
file write results_table "(`all_hh_new_veh_d_se_09')"  _tab
file write results_table " & " _tab
file write results_table "[`all_hh_new_veh_c_sd_17']" _tab
file write results_table " & " _tab
file write results_table "(`all_hh_new_veh_diff_se_2017')"  _tab
file write results_table " \\ " _n

*Blank row
file write results_table "\vspace{-0.2cm}" _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " \\ " _n



**allhh_new_bought
file write results_table "All Households" _tab
file write results_table " & " _tab
file write results_table "Newly-Purchased Count" _tab
file write results_table " & " _tab
file write results_table "`allhh_new_bought_c_m_09'" _tab
file write results_table " & " _tab
file write results_table "`allhh_new_bought_d_m_09'"  _tab
file write results_table " & " _tab
file write results_table "`allhh_new_bought_c_m_17'" _tab
file write results_table " & " _tab
file write results_table "`allhh_new_bought_d_m_17'"  _tab
file write results_table " \\ " _n
		
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "[`allhh_new_bought_c_sd_09']" _tab
file write results_table " & " _tab
file write results_table "(`allhh_new_bought_d_se_09')"  _tab
file write results_table " & " _tab
file write results_table "[`allhh_new_bought_c_sd_17']" _tab
file write results_table " & " _tab
file write results_table "(`allhh_new_bought_diff_se_2017')"  _tab
file write results_table " \\ " _n

*Blank row
file write results_table "\vspace{-0.2cm}" _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " \\ " _n



**single_mean_age
file write results_table "Single-Vehicle Household" _tab
file write results_table " & " _tab
file write results_table "Vehicle Age" _tab
file write results_table " & " _tab
file write results_table "`single_mean_age_c_m_09'" _tab
file write results_table " & " _tab
file write results_table "`single_mean_age_d_m_09'"  _tab
file write results_table " & " _tab
file write results_table "`single_mean_age_c_m_17'" _tab
file write results_table " & " _tab
file write results_table "`single_mean_age_d_m_17'"  _tab
file write results_table " \\ " _n
		
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "[`single_mean_age_c_sd_09']" _tab
file write results_table " & " _tab
file write results_table "(`single_mean_age_d_se_09')"  _tab
file write results_table " & " _tab
file write results_table "[`single_mean_age_c_sd_17']" _tab
file write results_table " & " _tab
file write results_table "(`single_mean_age_diff_se_2017')"  _tab
file write results_table " \\ " _n

*Blank row
file write results_table "\vspace{-0.2cm}" _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " \\ " _n



**multi_num_vehicle
file write results_table "Multi-Vehicle Households" _tab
file write results_table " & " _tab
file write results_table "Vehicle Count" _tab
file write results_table " & " _tab
file write results_table "`multi_num_vehicle_c_m_09'" _tab
file write results_table " & " _tab
file write results_table "`multi_num_vehicle_d_m_09'"  _tab
file write results_table " & " _tab
file write results_table "`multi_num_vehicle_c_m_17'" _tab
file write results_table " & " _tab
file write results_table "`multi_num_vehicle_d_m_17'"  _tab
file write results_table " \\ " _n
		
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "[`multi_num_vehicle_c_sd_09']" _tab
file write results_table " & " _tab
file write results_table "(`multi_num_vehicle_d_se_09')"  _tab
file write results_table " & " _tab
file write results_table "[`multi_num_vehicle_c_sd_17']" _tab
file write results_table " & " _tab
file write results_table "(`multi_num_vehicle_diff_se_2017')"  _tab
file write results_table " \\ " _n

*Blank row
file write results_table "\vspace{-0.2cm}" _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " \\ " _n



**multi_mean_age
file write results_table "Multi-Vehicle Households" _tab
file write results_table " & " _tab
file write results_table "Vehicle Age" _tab
file write results_table " & " _tab
file write results_table "`multi_mean_age_c_m_09'" _tab
file write results_table " & " _tab
file write results_table "`multi_mean_age_d_m_09'"  _tab
file write results_table " & " _tab
file write results_table "`multi_mean_age_c_m_17'" _tab
file write results_table " & " _tab
file write results_table "`multi_mean_age_d_m_17'"  _tab
file write results_table " \\ " _n
		
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "[`multi_mean_age_c_sd_09']" _tab
file write results_table " & " _tab
file write results_table "(`multi_mean_age_d_se_09')"  _tab
file write results_table " & " _tab
file write results_table "[`multi_mean_age_c_sd_17']" _tab
file write results_table " & " _tab
file write results_table "(`multi_mean_age_diff_se_2017')"  _tab
file write results_table " \\ " _n

*Blank row
file write results_table "\vspace{-0.2cm}" _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " \\ " _n



**num_new
file write results_table "Vehicle-Owning Households" _tab
file write results_table " & " _tab
file write results_table "New Vehicle Count" _tab
file write results_table " & " _tab
file write results_table "`num_new_c_m_09'" _tab
file write results_table " & " _tab
file write results_table "`num_new_d_m_09'"  _tab
file write results_table " & " _tab
file write results_table "`num_new_c_m_17'" _tab
file write results_table " & " _tab
file write results_table "`num_new_d_m_17'"  _tab
file write results_table " \\ " _n
		
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "[`num_new_c_sd_09']" _tab
file write results_table " & " _tab
file write results_table "(`num_new_d_se_09')"  _tab
file write results_table " & " _tab
file write results_table "[`num_new_c_sd_17']" _tab
file write results_table " & " _tab
file write results_table "(`num_new_diff_se_2017')"  _tab
file write results_table " \\ " _n

*Blank row
file write results_table "\vspace{-0.2cm}" _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " \\ " _n


**num_newly_bought
file write results_table "Vehicle-Owning Households" _tab
file write results_table " & " _tab
file write results_table "Newly-Purchased Count" _tab
file write results_table " & " _tab
file write results_table "`num_newly_bought_c_m_09'" _tab
file write results_table " & " _tab
file write results_table "`num_newly_bought_d_m_09'"  _tab
file write results_table " & " _tab
file write results_table "`num_newly_bought_c_m_17'" _tab
file write results_table " & " _tab
file write results_table "`num_newly_bought_d_m_17'"  _tab
file write results_table " \\ " _n
		
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "[`num_newly_bought_c_sd_09']" _tab
file write results_table " & " _tab
file write results_table "(`num_newly_bought_d_se_09')"  _tab
file write results_table " & " _tab
file write results_table "[`num_newly_bought_c_sd_17']" _tab
file write results_table " & " _tab
file write results_table "(`num_newly_bought_diff_se_2017')"  _tab
file write results_table " \\ " _n

*Blank row
file write results_table "\vspace{-0.2cm}" _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " \\ " _n


** max_veh_age
file write results_table "Vehicle-Owning Households" _tab
file write results_table " & " _tab
file write results_table "Maximum Vehicle Age" _tab
file write results_table " & " _tab
file write results_table "`max_veh_age_c_m_09'" _tab
file write results_table " & " _tab
file write results_table "`max_veh_age_d_m_09'"  _tab
file write results_table " & " _tab
file write results_table "`max_veh_age_c_m_17'" _tab
file write results_table " & " _tab
file write results_table "`max_veh_age_d_m_17'"  _tab
file write results_table " \\ " _n
		
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "[`max_veh_age_c_sd_09']" _tab
file write results_table " & " _tab
file write results_table "(`max_veh_age_d_se_09')"  _tab
file write results_table " & " _tab
file write results_table "[`max_veh_age_c_sd_17']" _tab
file write results_table " & " _tab
file write results_table "(`max_veh_age_diff_se_2017')"  _tab
file write results_table " \\ " _n

*Blank row
file write results_table "\vspace{-0.2cm}" _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " \\ " _n



**oldest_vmt
file write results_table "Vehicle-Owning Households" _tab
file write results_table " & " _tab
file write results_table "Oldest Vehicle VMT" _tab
file write results_table " & " _tab
file write results_table "`oldest_vmt_c_m_09'" _tab
file write results_table " & " _tab
file write results_table "`oldest_vmt_d_m_09'"  _tab
file write results_table " & " _tab
file write results_table "`oldest_vmt_c_m_17'" _tab
file write results_table " & " _tab
file write results_table "`oldest_vmt_d_m_17'"  _tab
file write results_table " \\ " _n
		
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "[`oldest_vmt_c_sd_09']" _tab
file write results_table " & " _tab
file write results_table "(`oldest_vmt_d_se_09')"  _tab
file write results_table " & " _tab
file write results_table "[`oldest_vmt_c_sd_17']" _tab
file write results_table " & " _tab
file write results_table "(`oldest_vmt_diff_se_2017')"  _tab
file write results_table " \\ " _n

*Blank row
file write results_table "\vspace{-0.2cm}" _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " & " _tab
file write results_table " \\ " _n


**min_miles
file write results_table "Vehicle-Owning Households" _tab
file write results_table " & " _tab
file write results_table "Minimum Vehicle VMT" _tab
file write results_table " & " _tab
file write results_table "`min_miles_c_m_09'" _tab
file write results_table " & " _tab
file write results_table "`min_miles_d_m_09'"  _tab
file write results_table " & " _tab
file write results_table "`min_miles_c_m_17'" _tab
file write results_table " & " _tab
file write results_table "`min_miles_d_m_17'"  _tab
file write results_table " \\ " _n
		
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "" _tab
file write results_table " & " _tab
file write results_table "[`min_miles_c_sd_09']" _tab
file write results_table " & " _tab
file write results_table "(`min_miles_d_se_09')"  _tab
file write results_table " & " _tab
file write results_table "[`min_miles_c_sd_17']" _tab
file write results_table " & " _tab
file write results_table "(`min_miles_diff_se_2017')"  _tab
file write results_table " \\ " _n

file close results_table
